package com.lsfb.signln.Statistics.EvectionSearch.SearchResult;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.My.MyDetailsBean;
import com.lsfb.signln.R;
import com.lsfb.signln.Statistics.EvectionSearch.SearchResult.PreviewPdf.PreviewPdfActivity;
import com.lsfb.signln.Utils.DialogBuilder;
import com.lsfb.signln.Utils.HttpDownloader;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 出差查询结果
 * Created by Administrator on 2018/1/31 0031.
 */
@ContentView(R.layout.aty_search_result)
public class SearchResultActivity extends LsfbActivity {

    /**
     * 查询的时间
     */
    @ViewInject(R.id.aty_search_text_date)
    private TextView text_date;

    /**
     * 用户名
     */
    @ViewInject(R.id.aty_search_text_name)
    private TextView text_name;

    /**
     * 部门
     */
    @ViewInject(R.id.aty_search_text_department)
    private TextView text_department;

    /**
     * 查询结果列表
     */
    @ViewInject(R.id.aty_search_rv)
    private RecyclerView rv_search;
    private List<SelfOutStatisticsBean.ListBean> data;
    private SelfOutStatisticsAdapter adapter;

    /**
     * 总计天数
     */
    @ViewInject(R.id.aty_search_text_day)
    private TextView text_day;

    /**
     * 总计钱
     */
    @ViewInject(R.id.aty_search_text_money)
    private TextView text_money;

    //开始时间
    private String start_time;
    //结束时间
    private String end_time;
    //差旅单号
    private String number;

    private MyProgressDialog dialog;

    protected DialogBuilder dialog_number;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "出差查询");
        dialog = new MyProgressDialog(this, "加载中......");
        if (getIntent().getStringExtra("start_time") != null && getIntent().getStringExtra("end_time") != null) {
            start_time = getIntent().getStringExtra("start_time");
            end_time = getIntent().getStringExtra("end_time");
        } else {
            T.showShort(this, "获取数据异常");
            finish();
            return;
        }

        text_date.setText("查询日期：" + start_time + " 至 " + end_time);

        data = new ArrayList<>();
        adapter = new SelfOutStatisticsAdapter(this, data, R.layout.item_search_evection);
        rv_search.setLayoutManager(new LinearLayoutManager(this));
        rv_search.setAdapter(adapter);


    }

    /**
     * 获取我的个人信息
     */
    private void getMyInfo() {
        HashMap hashMap = new HashMap<>();
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyInformatEvent<MyDetailsBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(MyInformatEvent<MyDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                text_name.setText("姓名：" + event.getData().getUserName());
                text_department.setText("部门：" + event.getData().getDepartName());
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 获取个人出差统计
     */
    private void getSelfOut() {
        HashMap hashMap = new HashMap();
        hashMap.put("startTime", start_time);
        hashMap.put("endTime", end_time);
        dialog.showdialog();
        new BaseInternet().getData(URLString.SHOWBUSSINESS, hashMap, SelfOutStatisticsBean.class, new SelfOutStatisticsEvent<SelfOutStatisticsBean>(), true);
    }

    @EventAnnotation
    public void eventReuslt(SelfOutStatisticsEvent<SelfOutStatisticsBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    text_money.setText(String.valueOf(event.getData().getMoney()));
                    text_day.setText(String.valueOf(event.getData().getCount()));

                    data.clear();
                    data.addAll(event.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    data.clear();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_search_btn_preview, R.id.aty_rl_statistics_selfout})
    public void onClick(View view) {
        switch (view.getId()) {
            //生成PDF预览
            case R.id.aty_search_btn_preview:
                //先下载保存，后显示预览
                showCancelDialog();
//                startActivity(new Intent(this, PreviewPdfActivity.class));
                break;
            case R.id.aty_rl_statistics_selfout:
                finish();
                break;
        }
    }

    class downloadPDFThread extends Thread {
        public void run() {
            HttpDownloader httpDownloader = new HttpDownloader();
            int downloadResult = httpDownloader.downloadFiles(
                    URLString.DOWNLOADPDF + "?usertoken=" + String.valueOf(Config.getUserId()) +
                            "&startTime=" + start_time + "&endTime=" + end_time + "&number=" + number,
                    "sign", "个人出差统计.pdf");
            LsfbLog.e("下载结果：" + downloadResult);


            switch (downloadResult) {
                case -1:        //下载失败
                    T.showShort(getApplicationContext(), "下载PDF失败");
                    break;
                case 0:
                case 1:
                    String downloadUrl = URLString.DOWNBYNUM + "?number=" + LittleUtils.xiazai;
                    startActivity(new Intent(SearchResultActivity.this, PreviewPdfActivity.class)
                            .putExtra("downloadUrl", downloadUrl));
                    break;
            }
        }
    }

    @Override
    public void initData() {
        getMyInfo();
        getSelfOut();
    }


    /**
     *
     */
    protected void showCancelDialog() {
        dialog_number = new DialogBuilder(this, R.layout.dialog_input_number) {
            @Override
            public void ViewListener(View contentview, final DialogBuilder dialogBuilder) {
                //输入差旅单号
                final EditText et_number = contentview.findViewById(R.id.dialog_et_number);
                contentview.findViewById(R.id.dialog_btn_sure).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (et_number.getText().toString().equals("请输入差旅单号")) {
                            T.showShort(getApplicationContext(), "请输入差旅单号");
                        } else {
                            number = et_number.getText().toString();
                            new downloadPDFThread().start();
                            dialog_number.hideDialog();
                        }
                    }
                });
            }
        };
        dialog_number.showDialog();
    }
}


